<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/03/28 11:14:11 $)
// $RCSfile: api_main.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("api");

require_once ("./includes/functions_xml.php");
require_once ("./includes/functions_api.php");
$xml = new XML;

$_data = getAPIData($_POST["xml"]);
if (!$_data)
{
	doAPIError(ERROR_INVALIDDATA, $_SWIFT["language"]["invaliddatareceived"]);
}


// api/index.php?_m=core&_a=api&xml=XMLHERE

/**
* ###############################################
* INSERT A NEW USER
* Action: insertuser
* Required Fields: fullname, email, password, enabled, usergroupid, ismanager
* Optional Fields: slaplanid
* ###############################################
*/
if ($_data["action"] == "insertuser")
{
	require_once ("./includes/functions_html.php");
	require_once ("./includes/functions_users.php");
	require_once ("./includes/functions_customfields.php");
	$template->loadLanguageSection("staff_users");
	$template->loadLanguageSection("staff_userpermissions");

	if (trim($_data["fullname"]) == "" || trim($_data["email"]) == "" || trim($_data["password"]) == "" || trim($_data["enabled"]) == "" || empty($_data["usergroupid"]) || empty($_data["ismanager"]))
	{
		doAPIError(ERROR_INVALIDDATA, $_SWIFT["language"]["requiredfieldempty"]);
	} else if (!isValidEmail($_POST["email"])) {
		doAPIError(ERROR_INVALIDDATA, $_SWIFT["language"]["invalidemail"]);
	} else {
		$userid = insertUser(intval($_data["enabled"]), $_data["email"], $_data["password"], $_data["usergroupid"], LOGINAPI_DEFAULT, 0, $_data["fullname"], 1, 0, false, 1, false, false, iif($_data["ismanager"]=="true", true, false));
		if (!$userid)
		{
			doAPIError(ERROR_INVALIDDATA, $_SWIFT["language"]["duplicateuseremail"]);
		} else {
			if (!empty($_data["slaplanid"]))
			{
				updateUserSLAPlan($userid, $_data["slaplanid"]);
			}

			$xml->addParentTag("api");
				$xml->addTag("status", "1");
				$xml->addParentTag("data");
					$xml->addTag("userid", $userid);
				$xml->endParentTag("data");
			$xml->endTag("api");
			$xml->echoXML();
			exit;
		}
	}
}
?>